<?php

namespace app\controller;

use app\service\PostRecord as PostRecordService;
use support\Request;
use think\Exception;

class PostRecord
{

    public $PostRecord;

    public function __construct(PostRecordService $postRecord)
    {

        $this->PostRecord = $postRecord;
    }

    /**
     * 创建文件记录操作
     * @param Request $request
     * @return \support\Response
     */
    public function create(Request $request)
    {
        $param = $request->all();
        $param['data']['user_id'] = $request->getCurrentUserId();
        $param['data']['tenant_id'] = $request->getTenantId();
        $res = $this->PostRecord->create($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 更新文件记录操作
     * @param Request $request
     * @return \support\Response
     */
    public function update(Request $request)
    {
        $params = $request->post();
        $params['data']['user_id'] = $request->getCurrentUserId();
        $res = $this->PostRecord->update($params['data']);
        return json(success_response('', $res));
    }

    /**
     * 文件记录查询
     * @return \support\Response
     */
    public function select(Request $request)
    {
        $params = $request->all();
        $res = $this->PostRecord->select($params['param'], $request->getCurrentUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * @return \support\Response|void
     * @throws \exception
     */
    public function find(Request $request)
    {
        try {
            $params = $request->all();
            $userId = $request->getCurrentUserId();
            $tenantId = $request->getTenantId();
            $res = $this->PostRecord->find($params['param']['filter'], $tenantId, $userId);
            return json(success_response('', $res));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除文件记录
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     * @throws \think\db\exception\DbException
     */
    public function delete(Request $request)
    {
        $params = $request->all();
        $res = $this->PostRecord->delete($params['param']);
        return json(success_response('', $res));
    }
}